﻿Ext.define('app.system.role.Index',
{
    extend: 'Ext.panel.Panel',
    border: true,
    layout: 'border',
    initComponent: function () {
        var me = this;
        me.items = [
            me.getRole(),
            me.getUserList()
        ];
        me.callParent(arguments);
    },
    getRole: function () {
        var me = this;
        if (!this.roleList) {
            this.roleList = new Ext.grid.Panel({
                border: true,
                region: 'west',
                title: '角色列表',
                width: 300,
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                plugins: [
                    Ext.create('Ext.grid.plugin.CellEditing', { clicksToEdit: 1 }),
                ],
                //multiColumnSort: true,
                forceFit: true,
                autoScroll: true,
                tbar: [
                                "->",
                    {
                        text: '新增角色',
                        icon: '/Content/images/add.png',
                        handler: function () {
                            var win = Ext.create("app.system.role.Create");
                            win.on("savesuccess", function () {
                                me.roleList.store.load();
                                win.close();
                            });
                            win.show();
                        }
                    },
                    {
                        text: '保存修改', icon: '/Content/images/page_save.png', handler: function () {
                            var arry = [];
                            var updates = me.roleList.store.getUpdatedRecords();
                            for (var i = 0; i < updates.length; i++) {
                                var record = updates[i];
                                arry.push({ key: record.data.id, value: record.data.displayName });
                            }
                            if (arry.length == 0) {
                                Ext.Msg.alert('系统提示', "没有需要保存的数据");
                                return false;
                            }
                            App.AjaxPost('/api/services/app/role/EditRole', arry, function (data) {
                                me.roleList.store.load();
                            });
                        }
                    }
                ],
                columns: [
                                 {
                                     xtype: 'actioncolumn',
                                     width: 100,
                                     header: localize('Operating'),
                                     items: [

                                     {
                                         icon: '/Content/images/delete.gif',
                                         tooltip: '删除',
                                         handler: function (grid, rowIndex, colIndex) {
                                             var record = grid.store.getAt(rowIndex);
                                             Ext.Msg.confirm('系统提示', '确定要删除系统角色吗？', function (btn) {
                                                 if (btn == 'yes') {
                                                     App.AjaxRemove('/api/services/app/role/DeleteRole', { id: record.data.id }, function (data) {
                                                         grid.store.removeAt(rowIndex);
                                                     });
                                                 }
                                             });
                                         }
                                     }, '-',
                                     {
                                         icon: '/Content/images/table_link.png',
                                         tooltip: '角色授权',
                                         handler: function (grid, rowIndex, colIndex) {
                                             var record = grid.store.getAt(rowIndex);
                                             var win = Ext.create("app.system.role.SetPermission", { title: '角色授权', roleId: record.data.id });
                                             win.on("savesuccess", function () {
                                                 win.close();
                                             });
                                             win.show();
                                         }
                                     }
                                     ]
                                 },
                     {
                         header: '角色', dataIndex: 'displayName', editor: {}
                     },
                     {
                         header: '创建时间', dataIndex: 'creationTime', renderer: function (value, p, record) {
                             return Ext.util.Format.date(value, 'Y-m-d')
                         }
                     }
                ],
                store: Ext.create("app.ux.PrmStore", {
                    url: '/api/services/app/role/GetAll', filter: {}
                    , remoteSort: false,
                    sorters: [{
                        //先按年龄降序  
                        property: 'displayName',
                        direction: 'ASC'
                    }]
                }),
                listeners: {
                    select: function (grid, record, index, opt)
                    {
                        me.getUserList().store.filter = { roleId: record.data.id };
                        me.getUserList().store.load();
                    }
                }
            });
        }
        return this.roleList;
    },
    getUserList: function () {
        var me = this;
        if (!this.userList) {
            this.userList = new Ext.grid.Panel({
                title: '角色用户',
                region: 'center',
                tbar: [
                    "->",
                    {
                        text: '新增成员', icon: '/Content/images/add.png', handler: function () {
                            var record = me.roleList.getSelection()[0];
                            if (!record) {
                                Ext.Msg.alert("系统提示", "请选择一个组织结构");
                                return false;
                            }
                            var win = Ext.create("app.system.role.RoleMember", { title: '选择组织成员', roleId: record.data.id });
                            win.on("savesuccess", function () {
                                me.userList.store.load();
                            });
                            win.show();
                        }
                    }
                ],
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                flex: 2,
                //multiColumnSort: true,
                forceFit: true,
                store: Ext.create("app.ux.PrmStore", {
                    url: '/api/services/app/role/GetUsers', autoLoad: false, remoteSort: false,
                    sorters: [{
                        //先按年龄降序  
                        property: 'userName',
                        direction: 'ASC'
                    }]
                }),
                columns: [
                    { header: '账号', dataIndex: 'userName' },
                    { header: '姓名', dataIndex: 'name' },
                    {
                        xtype: 'actioncolumn',
                        width: 35,
                        header: localize('Operating'),
                        items: [
                        {
                            icon: '/Content/images/delete.gif',
                            tooltip: '移除',
                            handler: function (grid, rowIndex, colIndex) {
                                var record = grid.store.getAt(rowIndex);
                                var role = me.roleList.getSelection()[0];
                                Ext.Msg.confirm('系统提示', '确定要移除角色用户吗？',
                                  function (btn) {
                                      if (btn == 'yes') {
                                          App.AjaxRemove('/api/services/app/user/RemoveRoleUser', { UserId: record.data.userId, RoleId: role.data.id }, function (data) {
                                              grid.store.removeAt(rowIndex);
                                          });
                                      }
                                  });
                            }
                        }
                        ]
                    }
                ]
            });
        }
        return this.userList;
    }
});
